package com.skillworld.webapp.web.pages.rest.tutorial;

import java.sql.Time;
import java.util.List;
import java.util.Locale;

import org.apache.tapestry5.annotations.ContentType;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.skillworld.webapp.model.tutorial.MarkFilter;
import com.skillworld.webapp.model.tutorial.MarkedTutorial.Mark;
import com.skillworld.webapp.model.tutorial.Tutorial;
import com.skillworld.webapp.model.tutorial.Tutorial.Status;
import com.skillworld.webapp.model.tutorial.TutorialOrder;
import com.skillworld.webapp.model.tutorialservice.TutorialService;
import com.skillworld.webapp.model.util.Pagination;
import com.skillworld.webapp.web.util.ErrorMessages;
import com.skillworld.webapp.web.util.Parameter;
import com.skillworld.webapp.web.util.UserSession;

@ContentType("text/xml")
public class SearchTutorials 
{
    private static final int DEFAULT_PAGE_NUMBER = 1;
    private static final int DEFAULT_PAGE_SIZE = 20;
     
    @SuppressWarnings("unused")
    @Property
    private String errorMessage = null;
    
    @SuppressWarnings("unused")
    @Property
    private List<Tutorial> tutorials;
    
    @Property
    private Pagination pagination;
    
    @SuppressWarnings("unused")
    @Property
    private Tutorial tut;   // loop iterator
    
    @Inject
    private TutorialService tutorialService;

    @SessionState(create=false)
    private UserSession userSession;
    
    private boolean userSessionExists;
    
    @Inject
    private Request request;
    

    void onPassivate()
    {
        return;
    }
    
    void onActivate()
    {
        if (!userSessionExists)
        {
            errorMessage = ErrorMessages.NOT_LOGGED_IN;
            return;
        }
        
        String query = request.getParameter("q");
        String deptParam = request.getParameter("dept");
        String langParam = request.getParameter("lang");
        String lengthParam = request.getParameter("length");
        String statusParam = request.getParameter("status");
        String authorParam = request.getParameter("author");
        String notauthorParam = request.getParameter("not-author");
        String markParam = request.getParameter("marked");
        String notincourseParam = request.getParameter("not-in-course");
        String orderByParam = request.getParameter("orderBy");
        String pageParam = request.getParameter("page");
        String psizeParam = request.getParameter("psize");
        
        if (query == null)
        {
            errorMessage = ErrorMessages.INVALID_ARGUMENTS;
            return;
        }
        
        try 
        {           
            Long dept = Parameter.maybeLong(deptParam);
            Locale lang = (langParam != null)
                              ? new Locale(langParam)
                              : null;
            Integer length = Parameter.maybeInt(lengthParam); 
            List<Status> status = (statusParam != null) 
                                   ? Parameter.commaSepStatuses(statusParam) 
                                   : null;
            Long author = Parameter.maybeLong(authorParam);
            List<Long> nonAuthors = (notauthorParam != null) 
                                    ? Parameter.commaSepIds(notauthorParam)
                                    : null;
            Mark mark = Parameter.maybeMark(markParam);
            MarkFilter marked = (mark != null)
                                ? new MarkFilter(userSession.getUserId(), mark)
                                : null;
            Long notInCourse = Parameter.maybeLong(notincourseParam);
            TutorialOrder orderBy = 
                Parameter.maybeTutorialOrder(orderByParam, 
                                             TutorialOrder.RATING);
            int page = Parameter.maybeInt(pageParam, DEFAULT_PAGE_NUMBER);
            int pageSize = Parameter.maybeInt(psizeParam, DEFAULT_PAGE_SIZE);
         
            int numberOfTutorials = 
                tutorialService.numberOfTutorials(query, 
                        dept, length, lang, 
                        status, 
                        author, nonAuthors, 
                        marked, notInCourse);
            pagination = new Pagination(numberOfTutorials, pageSize, page);
         
            if (pageSize <= 0 || !pagination.isValidPage())
            {
                errorMessage = ErrorMessages.INVALID_ARGUMENTS;
                return;
            }
            
            tutorials = tutorialService.searchTutorials(query, 
                                            dept, length, lang, 
                                            status, 
                                            author, nonAuthors, 
                                            marked, 
                                            notInCourse,
                                            orderBy, 
                                            pagination.getStartIndex(), 
                                            pagination.getCount());
        }
        catch (NumberFormatException e)
        { errorMessage = ErrorMessages.INVALID_ARGUMENTS; }
        catch (IllegalArgumentException e)
        { errorMessage = ErrorMessages.INVALID_ARGUMENTS; }
    }
}